package com.gao.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gao.domain.Cart;
import com.gao.domain.User;
import com.gao.service.CartService;
import com.gao.service.impl.CartServiceImpl;

/**
 * Servlet implementation class CartDeleteServlet
 */
@WebServlet("/CartDeleteServlet")
public class CartDeleteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String cidStr = request.getParameter("cid");
		CartService cartService = new CartServiceImpl();
		cartService.deleteByCid(Integer.parseInt(cidStr));
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		List<Cart> carts = cartService.getCartsByUid(user.getUid());
		//�����ܼƣ�����request
		double totalPrice = 0;
		for(Cart cart:carts){
			totalPrice = cart.getCartNum()*cart.getProduct().getPrice();
		}
		request.setAttribute("carts", carts);
		request.setAttribute("totalPrice", totalPrice);
		request.getRequestDispatcher("cart.jsp").forward(request, response);
	
	}

}
